<template>
  <div>
    <div class="play_bar">
      <audio
        :src="musicURL"
        ref="play"
        @ended="playEnd"
        @timeupdate="test"
      ></audio>
      <div class="songChang"><i class="iconfont icon-shangyishou"></i></div>
      <div class="playChang" @click="switchStatus">
        <i
          :class="
            isPlay
              ? 'iconfont icon-24gf-pauseCircle'
              : 'iconfont icon-24gf-playCircle'
          "
        ></i>
      </div>
      <div class="songChang"><i class="iconfont icon-xiayishou"></i></div>
    </div>
    <div class="play_progress"></div>
  </div>
</template>

<script>
export default {
  name: "Player",
  data() {
    return {
      // 是否为播放状态
      isPlay: false,
      // 音频总时长
      duration: null,
      // 音乐当前时长
      currentTime: null,
      // 当前播放音乐的url
      musicURL: '',
       // 是否继续播放音乐
      flag: false
    };
  },
  methods: {
    // 播放暂停按钮
     switchStatus() {
      if (this.isPlay) {
        this.$refs.play.pause();
      } else {
        this.updateNowPlay()
        setTimeout(() => {
          this.$refs.play.play();
        }, 500);
      }
      this.isPlay = !this.isPlay;
    },
    // 播放结束事件
    playEnd() {
      this.isPlay = false;
    },
    // 播放位置改变事件
    test() {
      // console.log("当前位置: ", this.$refs.play.currentTime);
    },
    // 刷新当前正在播放的音乐
    async updateNowPlay() {
      const { data: res } = await this.$http.get(
          `/song/url?id=${window.sessionStorage.getItem("music_id")}&timestamp=${this.$timestamp()}`
        );
        console.log(res);
        this.musicURL = res.data[0].url
    }
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
.play_bar {
  height: 100%;
  // background-color: rgb(144, 172, 189);
  display: flex;
  justify-content: center;
  align-items: center;
  .songChang {
    height: 30px;
    margin: 0 40px;
    cursor: pointer;
  }
  .playChang {
    cursor: pointer;
  }
}
.play_progress {
  height: 10px;
  background-color: red;
}
</style>